<html>
<head><meta charset="utf-8"><title>vec vs array behavior · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/vec.20vs.20array.20behavior.html">vec vs array behavior</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217993640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/vec%20vs%20array%20behavior/near/217993640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maximilian Hristache <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/vec.20vs.20array.20behavior.html#217993640">(Nov 26 2020 at 12:04)</a>:</h4>
<p>Hi, can someone please help me understand why the linked code borrow checks when using an array but fails when using a Vec? <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d07c282391eefa537135952a85d7db2f">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d07c282391eefa537135952a85d7db2f</a></p>
<p>Thank you</p>



<a name="217994045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/vec%20vs%20array%20behavior/near/217994045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maximilian Hristache <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/vec.20vs.20array.20behavior.html#217994045">(Nov 26 2020 at 12:09)</a>:</h4>
<p>Is it because the vec can be mutated in a way that the entries can be removed which is not the case for the array?</p>



<a name="217994191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/vec%20vs%20array%20behavior/near/217994191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maximilian Hristache <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/vec.20vs.20array.20behavior.html#217994191">(Nov 26 2020 at 12:11)</a>:</h4>
<p>But if that is the case, shouldn't the compiler realize that I am just updating the existing entries, I am not mutating the vec in a way that can cause memory safety?</p>



<a name="217997273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/vec%20vs%20array%20behavior/near/217997273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/vec.20vs.20array.20behavior.html#217997273">(Nov 26 2020 at 12:46)</a>:</h4>
<p>The array indexing for arrays is built into the compiler. For <code>Vec</code> it calls the <code>index</code>/<code>index_mut</code> methods, which borrow the whole <code>Vec</code>.</p>



<a name="218021507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/vec%20vs%20array%20behavior/near/218021507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/vec.20vs.20array.20behavior.html#218021507">(Nov 26 2020 at 16:49)</a>:</h4>
<p>You can use <code>let slice = &amp;mut *store</code> to get back to built-in indexing.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>